package com.qianyitian.hope2.analyzer.service;

import com.qianyitian.hope2.analyzer.config.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service("redisStorageService")
public class RedisStorageService implements IReportStorageService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private StringRedisTemplate redisTemplate;

    public RedisStorageService() {
    }


    private String getStatisticsPrefix() {
        return Constant.STATISTICS;
    }

    private String getAnalysisPrefix() {
        return Constant.REPORT;
    }

    private void storeFile(String prefix, String fileName, String content) {
        String key = prefix + fileName;
        redisTemplate.opsForValue().set(key, content);
    }

    private String getFile(String prefix, String fileName) {
        String key = prefix + fileName;
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void storeAnalysis(String fileName, String content) {
        storeFile(getAnalysisPrefix(), fileName, content);
    }

    @Override
    public String getAnalysis(String fileName) {
        return getFile(getAnalysisPrefix(), fileName);
    }

    @Override
    public void storeStatistics(String fileName, String content) {
        storeFile(getStatisticsPrefix(), fileName, content);
    }

    @Override
    public String getStatistics(String fileName) {
        return getFile(getStatisticsPrefix(), fileName);
    }
}
